package pers.wq;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import pers.wq.model.dao.QueryDao;
import pers.wq.util.Helper;

public class BaseModel {
	protected String table;
	public BaseModel(String table){
		this.table = table;
	}
	public BaseModel(){
		
	}
	
	protected boolean insert(Map<String,Object> map){
		return insert(this.table,map);
	}
	
	protected boolean update(Map<String,Object> map,String where){
		return update(this.table,map,where);
	}
	
	protected boolean delete(String where){
		return delete(this.table,where);
	}
	
	protected List<Map<String,Object>> select(String column,String condition,String order,String limit){
		System.out.println("BaseModel select : in [" + table);
		return select(column,this.table,condition,order,limit);
	}
	
	public static boolean insert(String table,Map<String,Object> map){
		StringBuilder sb = new StringBuilder("INSERT INTO ");
		sb.append(table + " ");
		StringBuilder columns = new StringBuilder("(");
		StringBuilder contents = new StringBuilder("(");
		Iterator<String> iterator = map.keySet().iterator();
		String key;
		while(iterator.hasNext()){
			key = iterator.next();
			columns.append(key + ",");
			if(map.get(key) instanceof String){
				contents.append("'" + map.get(key).toString() + "',");
			}else{
				contents.append(map.get(key).toString() + ",");
			}
		}
		columns = columns.deleteCharAt(columns.length()-1);
		contents = contents.deleteCharAt(contents.length()-1);
		columns.append(')');
		contents.append(')');
		sb.append(columns);
		sb.append(" VALUES ");
		sb.append(contents);
		return QueryDao.query(sb.toString());
	}
	
	public static boolean update(String table,Map<String,Object> map,String where){
		StringBuilder sb = new StringBuilder("UPDATE ");
		sb.append(table);
		sb.append(" SET ");
		Iterator<String> iterator = map.keySet().iterator();
		String key;
		while(iterator.hasNext()){
			key = iterator.next();
			sb.append(key + "=");
			if(map.get(key) instanceof String){
				sb.append("'"+map.get(key).toString()+"'");
			}else{
				sb.append(map.get(key).toString());
			}
			sb.append(",");
		}
		sb = sb.deleteCharAt(sb.length()-1);
		if(!Helper.strIsNull(where)){
			sb.append(" WHERE ");
			sb.append(where);
		}
		return QueryDao.query(sb.toString());
	}
	
	public static boolean delete(String table,String where){
		StringBuilder sb = new StringBuilder("DELETE FROM ");
		sb.append(table);
		sb.append(" WHERE ");
		sb.append(where);
		return QueryDao.query(sb.toString());
	}
	
	public static List<Map<String,Object>> select(String column,String table,String condition,String order,String limit){
		if(Helper.strIsNull(table)){
			return null;
		}
		StringBuilder sb = new StringBuilder("SELECT ");
		if(Helper.strIsNull(column)){
			sb.append("*");
		}else{
			sb.append(column);
		}
		sb.append(" FROM " + table + " WHERE ");
		if(Helper.strIsNull(condition)){
			sb.append("1=1");
		}else{
			sb.append(condition);
		}
		if(!Helper.strIsNull(order)){
			sb.append(" ORDER BY " + order);
		}
		if(!Helper.strIsNull(limit)){
			sb.append(" LIMIT " + limit);
		}
		return QueryDao.selectQuery(sb.toString());
	}
}
